$(document).ready(function(){
	//登录
	$('#login').click(function(){
		if( !validateLogin() ){
			return;
		}
							   
		var username = $('.login input[name=username]').val();
		var pwd = $('.login input[name=pwd]').val();

		$.ajax({
			url : 'login.asp',
			type : 'POST',
			data : {
				username : username,
				pwd : pwd,
			},
			success : function(data){
				switch( data ){
					case 'error':
						infoModal("错误","用户名或者密码错误");
						break;
					case 'success':
						changeMode(username);
						break;
				}
			},
		});
	});

	//注册
	$('#register').click(function(){
		if( !validateRegister() ){
			return;
		}
		var username = $('.register input[name=username]').val();
		var pwd = $('.register input[name=pwd]').val();
		var pwd2 = $('.register input[name=pwd2]').val();

		$.ajax({
			url : 'register.asp',
			type : 'POST',
			data : {
				username : username,
				pwd : pwd,
				pwd2 : pwd2,
			},
			success : function(data){
				switch( data ){
					case 'password_differ':
						infoModal("错误","两次密码输入不同");
						break;
					case 'username_exist':
						infoModal("错误","用户名已存在");
						break;
					case 'success':
						changeMode(username);
						break;
				}
			},
		});
	});
	//信息显示modal
	function infoModal(header,body){
		var html = new EJS({url: 'infoModal.ejs'}).render({});
		$('.modal').html(html);
		$('.modal .modal-header h3').html(header);
		$('.modal .modal-body p').html(body);
		$('.modal').modal('toggle');
		//点击关闭退出modal
		$('.close,.confirm').click(function(){
			$('.modal').modal('hide');
		});
	}
	//通讯录输入Modal
	function addressBookModal(header,data){
		var html = new EJS({url: 'AddressBookModal.ejs'}).render(data);
		$('.modal').html(html);
		$('.modal .modal-header h3').html(header);
		$('.modal').modal('show');
		//点击关闭退出modal
		$('.close').click(function(){
			$('.modal').modal('hide');
		});
	}
	//登录后显示search，隐藏camera和sidebar
	//同时显示用户信息
	function changeMode(username){
		//首先获得通讯录
		getAddressBook();
		$('.camera,.sidebar').fadeOut('slow',function(){
			$('.search').fadeIn('slow');											  
		});
		$('#username').html(username);
	}
	//获取通讯录信息
	function getAddressBook(){
		$.ajax({
			url : 'getAddressBook.asp',
			success : function(data){
				var persons = dataToObject(data);
				var html = new EJS({url: 'tbody.ejs'}).render({persons:persons});
				$('tbody').html(html);
				addListener();
			},
		});
	}
	//检索通讯录信息
	function searchAddressBook(keyword){
		$.ajax({
			url : 'searchAddress.asp',
			type : 'POST',
			data : {
				keyword : keyword,
			},
			success : function(data){
				var persons = dataToObject(data);
				var html = new EJS({url: 'tbody.ejs'}).render({persons:persons});
				$('tbody').html(html);
				addListener();
			},
		});
	}
	//对象化数据
	function dataToObject(data){
		var rows = data.split(';');
		var fields;
		var persons = [];
		for(var i=0;i<rows.length;i++){
			fields = rows[i].split(',');
			var person = {};
			for(var j=0;j<fields.length;j++){
				var field = fields[j].split('=');
				person[field[0]] = field[1];
			}
			persons.push(person);
		}
		return persons;
	}
	//为搜索页面增加事件监听
	function addListener(){
		//增加通讯录记录
		$('.add').click(function(){
			addressBookModal("增加通讯录条目",{id:'',name:'',telNo:'',email:'',address:''});
			//提交表单
			$('.confirm').click(function(){
				if( !validateAddress() ){
					return;
				}
				var username = $('.addressBookModal input[name=username]').val();
				var telNo = $('.addressBookModal input[name=telNo]').val();				
				var email = $('.addressBookModal input[name=email]').val();				
				var address = $('.addressBookModal input[name=address]').val();							
				//请求服务
				$.ajax({
					url : 'addAddress.asp',
					type : 'POST',
					data : {
						username : username,
						telNo : telNo,
						email : email,
						address : address,
					},
					success : function(data){
						//后续增加条目出来
						getAddressBook();
						$('.modal').modal('hide');
					},
				});										 
			});
		});
		//修改通讯录记录
		$('.modify').click(function(){
			var data = {};
			var tds = $(this).parent().parent().children('td');
			data.id = tds[0].innerHTML;
			data.name = tds[1].innerHTML;
			data.telNo = tds[2].innerHTML;
			data.email = tds[3].innerHTML;
			data.address = tds[4].innerHTML;
			
			addressBookModal("修改通讯录条目",data);
			//提交表单
			$('.confirm').click(function(){
				if( !validateAddress() ){
					return;
				}
				var id = $('.addressBookModal input[name=id]').val();
				var username = $('.addressBookModal input[name=username]').val();
				var telNo = $('.addressBookModal input[name=telNo]').val();				
				var email = $('.addressBookModal input[name=email]').val();				
				var address = $('.addressBookModal input[name=address]').val();							
				//请求服务
				$.ajax({
					url : 'modifyAddress.asp',
					type : 'POST',
					data : {
						id : id,
						username : username,
						telNo : telNo,
						email : email,
						address : address,
					},
					success : function(data){
						
						getAddressBook();
						$('.modal').modal('hide');
					},
				});										 
			});
		});	
		//删除通讯录记录
		$('.remove').click(function(){
			infoModal("删除通讯录条目","你确定要删除该通讯录条目吗？");
			var tds = $(this).parent().parent().children('td');
			var id = tds[0].innerHTML;
			$('.confirm').click(function(){
				//请求服务
				$.ajax({
					url : 'removeAddress.asp',
					type : 'POST',
					data : {
						id : id,
					},
					success : function(data){
						
						getAddressBook();
						$('.modal').modal('hide');
					},
				});	
			});
		});
	}
	//删除通讯录记录，不需要在addListener中
	$('.btn-search').click(function(){
		var keyword = $('#keyword').val();
		searchAddressBook(keyword);
	});
	//表单客户端验证
	function validateLogin(){
		var username = $('.login input[name=username]').val();
		var pwd = $('.login input[name=pwd]').val();
		if( username == "" ){
			alert("用户名不能为空");
			return false;
		}
		if( pwd == "" ){
			alert("密码不能为空");
			return false;
		}
		return true;
	}
	function validateRegister(){
		var username = $('.register input[name=username]').val();
		var pwd = $('.register input[name=pwd]').val();
		var pwd2 = $('.register input[name=pwd2]').val();
		if( username == "" ){
			alert("用户名不能为空");
			return false;
		}
		if( pwd !== pwd2 ){
			alert("两次密码输入不同");
			return false;
		}
		return true;
	}
	function validateAddress(){
		var username = $('.addressBookModal input[name=username]').val();
		var telNo = $('.addressBookModal input[name=telNo]').val();				
		var email = $('.addressBookModal input[name=email]').val();				
		var address = $('.addressBookModal input[name=address]').val();	
		if( username == "" ){
			alert("用户名不能为空");
			return false;
		}
		if( telNo == "" ){
			alert("联系方式不能为空");
			return false;
		}
		if( email == "" ){
			alert("电子有限不能为空");
			return false;
		}
		if( address == "" ){
			alert("地址不能为空");
			return false;
		}
		return true;
	}
});
